<!--
 * @Description: 
 * @Date: 2024-01-26 14:53:41
 * @LastEditTime: 2024-02-26 15:49:45
-->
<template>
  <div class="inventorySuggest">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      v-show="showSearch"
      :inline="true"
    >
      <el-form-item label="届次选择" prop="seId">
        <el-select
          v-model="queryParams.seId"
          placeholder="请选择所在届次"
          style="width: 100%"
          @change="replacePerChange($event)"
        >
          <el-option
            v-for="dict in sessionList"
            :key="dict.seId"
            :label="dict.label"
            :value="dict.seId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="题目" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入题目"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          style="width: 100%"
          @change="replacePerChange($event)"
        >
          <el-option
            v-for="dict in dict.type.sys_suggest_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="领衔代表" prop="leaderUser">
        <el-input
          v-model="queryParams.leaderUser"
          placeholder="请输入题目"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="附议代表" prop="followerUser">
        <el-input
          v-model="queryParams.followerUser"
          placeholder="请输入题目"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="代表团" prop="deId">
        <el-select
          v-model="queryParams.deId"
          placeholder="请选择状态"
          style="width: 100%"
          @change="replacePerChange($event)"
        >
          <el-option
            v-for="dict in delegationList"
            :key="dict.deId"
            :label="dict.name"
            :value="dict.deId"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="建议号" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入题目"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['suggest:inventorySuggest:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['suggest:inventorySuggest:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['suggest:inventorySuggest:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-table
      v-loading="loading"
      :data="suggestList"
      border
      @selection-change="handleSelectionChange"
      style="width: 100%"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column
        prop="suggestInfo.suId"
        label="建议号"
        align="center"
        width="120px"
      > -->
      <!-- </el-table-column> -->
      <el-table-column
        prop="suggestInfo.title"
        label="题目"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column prop="deptId" label="承办单位" align="center">
        <template slot-scope="scope">
          {{ scope.row.deptId | depName(userList) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="address"
        label="代表团"
        align="center"
        width="120px"
      >
        <template slot-scope="scope">
          {{
            scope.row.suggestInfo.leaderUser.deId | delegation(delegationList)
          }}
        </template>
      </el-table-column>
      <el-table-column label="领衔代表" align="center" width="120px">
        <template slot-scope="scope">
          {{ scope.row.suggestInfo.leaderUser.userName }}
        </template>
      </el-table-column>

      <el-table-column prop="status" label="状态" align="center" width="120px">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_suggest_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column prop="address" label="操作" align="center" width="250px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleSkip(scope.row)"
            v-hasPermi="['suggest:inventorySuggest:info']"
            >查看详情</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleCheck(scope.row)"
            v-hasPermi="['suggest:inventorySuggest:hedui']"
            v-show="scope.row.status == 0"
            >核对</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAssign(scope.row)"
            v-show="scope.row.status == 1"
            v-hasPermi="['suggest:inventorySuggest:jiaoban']"
            >交办</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAssign(scope.row)"
            v-hasPermi="['suggest:inventorySuggest:biangeng']"
            v-show="scope.row.status == 3"
            >变更处理</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['suggest:inventorySuggest:edit']"
            v-show="scope.row.status == '0'"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <checkDialog
      v-if="checkVisible"
      v-model="checkVisible"
      :proId="proId"
    ></checkDialog>
    <assignDialog
      v-if="assignVisible"
      v-model="assignVisible"
      :proId="proId"
    ></assignDialog>
  </div>
</template>

<script>
import {
  getProcessInfoList,
  userListByRoleId,
  delProcessInfo,
} from "@/api/suggest/index";
import { addDelegationList } from "@/api/dbxx/dbxxgroup/index";
import { getAuthRole } from "@/api/system/user";
import checkDialog from "./dialog/checkDialog.vue";
import assignDialog from "./dialog/assignDialog.vue";
import { mapGetters } from "vuex";
import { addSessionList } from "@/api/dbxx/dbxxLevel/index";
export default {
  name: "inventorySuggest",
  dicts: ["sys_suggest_status"],
  computed: {
    ...mapGetters(["userId", "roles"]),
  },
  components: {
    checkDialog,
    assignDialog,
  },
  data() {
    return {
      loading: false,
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      showSearch: true,
      queryParams: {
        seId: undefined,
        title: undefined,
        submitStatus: undefined,
        leaderId: undefined,
        deId: undefined,
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      sessionList: [],
      delegationList: [],
      suggestList: [],
      userList: [],
      checkVisible: false,
      assignVisible: false,
      proId: null,
      names: [],
    };
  },
  filters: {
    delegation(value, delegationList) {
      return delegationList.filter((item) => item?.deId == value)[0]?.name;
    },
    depName(value, userList) {
      let arr = userList.filter((item) => item?.userId == value);
      return arr?.length ? arr[0]?.userName : "暂无";
    },
  },
  created() {
    //获取userId 根据不同的角色id拉取不同的建议清单
    let chSupervise = this.roles.some((item) => item == "duban");
    if (chSupervise) {
      this.queryParams.chSuperviseId = this.userId;
    }
    this.getList();
    addDelegationList().then((response) => {
      this.delegationList = response.rows;
    });
    addSessionList().then((response) => {
      this.sessionList = response.rows.map((item) => {
        return {
          label:
            item.area +
            `第${item.session}届人民代表大会` +
            `第${item.orderName}次会议` +
            `(${item.annual})`,
          seId: Number(item.seId),
        };
      });
    });
    userListByRoleId("9").then((res) => {
      this.userList = res.data.users;
    });
  },
  methods: {
    /**
     * @description: 查看详情
     * @return {*}
     */
    handleDetails() {},
    /**
     * @description:  核对
     * @return {*}
     */
    handleCheck(row) {
      this.checkVisible = true;
      this.proId = row.proId;
    },
    /**
     * @description:  交办
     * @return {*}
     */
    handleAssign(row) {
      this.assignVisible = true;
      this.proId = row.proId;
    },
    /**
     * @description:  变更处理
     * @return {*}
     */
    handleAlteration() {},
    /**
     * @description:  修改
     * @return {*}
     */
    handleUpdate(row) {
      this.$router.push({
        path: "/suggest/suggestInfo",
        query: {
          proId: row.proId,
          type: "writeable",
          add: true,
        },
      });
    },
    handleSkip(row) {
      this.$router.push({
        path: "/suggest/suggestInfo",
        query: {
          proId: row.proId,
          type: "readOnly",
        },
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.queryParams.status
        ? (this.queryParams.proStatus = [this.queryParams.status])
        : (this.queryParams.proStatus = undefined);
      this.queryParams.status = undefined;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleDelete(row) {
      const userId = row?.proId || this.ids;
      const name = row?.suggestInfo?.title || this.names;
      this.$modal
        .confirm('是否确认删除代表名称为"' + name + '"的数据项？')
        .then(function () {
          return delProcessInfo(userId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    handleExport() {
      this.download(
        "/system/processInfo/export",
        {
          ...this.queryParams,
        },
        `代表建议_${new Date().getTime()}.xlsx`
      );
    },
    replacePerChange(val) {
      this.$forceUpdate();
    },
    getList() {
      this.loading = true;
      getProcessInfoList({ ...this.queryParams }).then((res) => {
        this.suggestList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.proId);
      this.names = selection.map((item) => item.suggestInfo.title);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
  },
};
</script>

<style lang="scss" scoped>
.inventorySuggest {
  padding: 20px;
}
</style>
